/**
 * Copyright Statement:
 * This software and related documentation (ESWIN SOFTWARE) are protected under relevant copyright laws.
 * The information contained herein is confidential and proprietary to
 * Beijing ESWIN Computing Technology Co., Ltd.(ESWIN)and/or its licensors.
 * Without the prior written permission of ESWIN and/or its licensors, any reproduction, modification,
 * use or disclosure Software, and information contained herein, in whole or in part, shall be strictly prohibited.
 *
 * Copyright ©[2023] [Beijing ESWIN Computing Technology Co., Ltd.]. All rights reserved.
 *
 * @file buzzer_baremetal.c
 * @brief 蜂鸣器控制模块实现文件 - Baremetal版本
 * @author user
 * @date 2024-01-25
 *
 * @details 蜂鸣器控制功能实现，适用于Baremetal环境
 */

#include "buzzer_baremetal.h"
#include "basic_api.h"

/* 预定义旋律 - 两只老虎 */
const buzzer_note_t melody_two_tigers[] = {
    // 两只老虎，两只老虎
    {NOTE_C4, BEAT_QUARTER}, {NOTE_D4, BEAT_QUARTER}, {NOTE_E4, BEAT_QUARTER}, {NOTE_C4, BEAT_QUARTER},
    {NOTE_C4, BEAT_QUARTER}, {NOTE_D4, BEAT_QUARTER}, {NOTE_E4, BEAT_QUARTER}, {NOTE_C4, BEAT_QUARTER},
    
    // 跑得快，跑得快
    {NOTE_E4, BEAT_QUARTER}, {NOTE_F4, BEAT_QUARTER}, {NOTE_G4, BEAT_HALF},
    {NOTE_E4, BEAT_QUARTER}, {NOTE_F4, BEAT_QUARTER}, {NOTE_G4, BEAT_HALF},
    
    // 一只没有眼睛，一只没有尾巴
    {NOTE_G4, BEAT_EIGHTH}, {NOTE_A4, BEAT_EIGHTH}, {NOTE_G4, BEAT_EIGHTH}, {NOTE_F4, BEAT_EIGHTH}, {NOTE_E4, BEAT_QUARTER}, {NOTE_C4, BEAT_QUARTER},
    {NOTE_G4, BEAT_EIGHTH}, {NOTE_A4, BEAT_EIGHTH}, {NOTE_G4, BEAT_EIGHTH}, {NOTE_F4, BEAT_EIGHTH}, {NOTE_E4, BEAT_QUARTER}, {NOTE_C4, BEAT_QUARTER},
    
    // 真奇怪，真奇怪
    {NOTE_C4, BEAT_QUARTER}, {NOTE_G4, BEAT_QUARTER}, {NOTE_C4, BEAT_HALF},
    {NOTE_C4, BEAT_QUARTER}, {NOTE_G4, BEAT_QUARTER}, {NOTE_C4, BEAT_HALF}
};

/* 预定义旋律 - 小星星 */
const buzzer_note_t melody_twinkle_star[] = {
    // 一闪一闪亮晶晶
    {NOTE_C4, BEAT_QUARTER}, {NOTE_C4, BEAT_QUARTER}, {NOTE_G4, BEAT_QUARTER}, {NOTE_G4, BEAT_QUARTER},
    {NOTE_A4, BEAT_QUARTER}, {NOTE_A4, BEAT_QUARTER}, {NOTE_G4, BEAT_HALF},
    
    // 满天都是小星星
    {NOTE_F4, BEAT_QUARTER}, {NOTE_F4, BEAT_QUARTER}, {NOTE_E4, BEAT_QUARTER}, {NOTE_E4, BEAT_QUARTER},
    {NOTE_D4, BEAT_QUARTER}, {NOTE_D4, BEAT_QUARTER}, {NOTE_C4, BEAT_HALF},
    
    // 挂在天空放光明
    {NOTE_G4, BEAT_QUARTER}, {NOTE_G4, BEAT_QUARTER}, {NOTE_F4, BEAT_QUARTER}, {NOTE_F4, BEAT_QUARTER},
    {NOTE_E4, BEAT_QUARTER}, {NOTE_E4, BEAT_QUARTER}, {NOTE_D4, BEAT_HALF},
    
    // 好像许多小眼睛
    {NOTE_G4, BEAT_QUARTER}, {NOTE_G4, BEAT_QUARTER}, {NOTE_F4, BEAT_QUARTER}, {NOTE_F4, BEAT_QUARTER},
    {NOTE_E4, BEAT_QUARTER}, {NOTE_E4, BEAT_QUARTER}, {NOTE_D4, BEAT_HALF},
    
    // 一闪一闪亮晶晶
    {NOTE_C4, BEAT_QUARTER}, {NOTE_C4, BEAT_QUARTER}, {NOTE_G4, BEAT_QUARTER}, {NOTE_G4, BEAT_QUARTER},
    {NOTE_A4, BEAT_QUARTER}, {NOTE_A4, BEAT_QUARTER}, {NOTE_G4, BEAT_HALF},
    
    // 满天都是小星星
    {NOTE_F4, BEAT_QUARTER}, {NOTE_F4, BEAT_QUARTER}, {NOTE_E4, BEAT_QUARTER}, {NOTE_E4, BEAT_QUARTER},
    {NOTE_D4, BEAT_QUARTER}, {NOTE_D4, BEAT_QUARTER}, {NOTE_C4, BEAT_HALF}
};

/* 预定义旋律 - 卡农 (简化版) */
const buzzer_note_t melody_canon[] = {
    // 第一段
    {NOTE_D4, BEAT_HALF}, {NOTE_A4, BEAT_QUARTER}, {NOTE_B4, BEAT_QUARTER}, {NOTE_F4, BEAT_QUARTER}, 
    {NOTE_G4, BEAT_QUARTER}, {NOTE_D4, BEAT_QUARTER}, {NOTE_G4, BEAT_QUARTER}, {NOTE_A4, BEAT_HALF},
    
    // 第二段
    {NOTE_D4, BEAT_HALF}, {NOTE_A4, BEAT_QUARTER}, {NOTE_B4, BEAT_QUARTER}, {NOTE_F4, BEAT_QUARTER},
    {NOTE_G4, BEAT_QUARTER}, {NOTE_D4, BEAT_QUARTER}, {NOTE_G4, BEAT_QUARTER}, {NOTE_A4, BEAT_HALF},
    
    // 第三段 (高音部分)
    {NOTE_D5, BEAT_QUARTER}, {NOTE_C5, BEAT_QUARTER}, {NOTE_B4, BEAT_QUARTER}, {NOTE_A4, BEAT_QUARTER},
    {NOTE_B4, BEAT_QUARTER}, {NOTE_C5, BEAT_QUARTER}, {NOTE_B4, BEAT_QUARTER}, {NOTE_A4, BEAT_QUARTER},
    
    // 结尾
    {NOTE_A4, BEAT_QUARTER}, {NOTE_G4, BEAT_QUARTER}, {NOTE_F4, BEAT_QUARTER}, {NOTE_G4, BEAT_QUARTER},
    {NOTE_A4, BEAT_QUARTER}, {NOTE_D4, BEAT_QUARTER}, {NOTE_G4, BEAT_HALF}, {NOTE_D4, BEAT_WHOLE}
};

/* 提示音旋律 */
const buzzer_note_t melody_startup[] = {
    {NOTE_C4, BEAT_EIGHTH}, {NOTE_E4, BEAT_EIGHTH}, {NOTE_G4, BEAT_EIGHTH}, {NOTE_C5, BEAT_QUARTER}
};

const buzzer_note_t melody_success[] = {
    {NOTE_C5, BEAT_EIGHTH}, {NOTE_E5, BEAT_EIGHTH}, {NOTE_G5, BEAT_QUARTER}
};

const buzzer_note_t melody_error[] = {
    {NOTE_G4, BEAT_QUARTER}, {NOTE_F4, BEAT_QUARTER}, {NOTE_E4, BEAT_HALF}
};

const buzzer_note_t melody_warning[] = {
    {NOTE_A4, BEAT_EIGHTH}, {NOTE_REST, BEAT_EIGHTH}, {NOTE_A4, BEAT_EIGHTH}, {NOTE_REST, BEAT_EIGHTH},
    {NOTE_A4, BEAT_EIGHTH}, {NOTE_REST, BEAT_EIGHTH}
};

/* 私有函数声明 */
static void buzzer_pwm_output(uint16_t frequency, uint16_t duration);

/**
 * @brief 初始化蜂鸣器
 */
void Buzzer_Init(void)
{
    // 调试：蜂鸣器初始化开始指示 - LED快速闪烁
    for (int i = 0; i < 5; i++) {
        // 同时点亮所有LED
        PINS_DRV_WritePin(PORTC, 18, 1);  // LED2
        PINS_DRV_WritePin(PORTC, 28, 1);  // LED3
        PINS_DRV_WritePin(PORTD, 14, 1);  // LED4
        BASIC_DelayMs(100);

        // 同时熄灭所有LED
        PINS_DRV_WritePin(PORTC, 18, 0);  // LED2
        PINS_DRV_WritePin(PORTC, 28, 0);  // LED3
        PINS_DRV_WritePin(PORTD, 14, 0);  // LED4
        BASIC_DelayMs(100);
    }

    BASIC_DelayMs(500);

    // 蜂鸣器初始化完成指示 - LED慢闪3次
    for (int i = 0; i < 3; i++) {
        PINS_DRV_WritePin(PORTC, 18, 1);  // LED2
        PINS_DRV_WritePin(PORTC, 28, 1);  // LED3
        PINS_DRV_WritePin(PORTD, 14, 1);  // LED4
        BASIC_DelayMs(500);

        PINS_DRV_WritePin(PORTC, 18, 0);  // LED2
        PINS_DRV_WritePin(PORTC, 28, 0);  // LED3
        PINS_DRV_WritePin(PORTD, 14, 0);  // LED4
        BASIC_DelayMs(500);
    }

    // GPIO引脚初始化在pin_config.c中完成
    // 这里只需要确保引脚为低电平
    PINS_DRV_WritePin(BUZZER_PORT, BUZZER_PIN, 0);
}

/**
 * @brief 蜂鸣器发声
 */
void Buzzer_Tone(uint16_t frequency, uint16_t duration)
{
    // 调试：LED2 (PORTC18) 快闪表示蜂鸣器被调用
    PINS_DRV_WritePin(PORTC, 18, 1);  // 点亮LED2
    BASIC_DelayMs(100);
    PINS_DRV_WritePin(PORTC, 18, 0);  // 熄灭LED2

    // 调试：LED3 (PORTC28) 根据频率闪烁
    if (frequency > 1000) {
        // 高频率 - LED3快闪2次
        for (int i = 0; i < 2; i++) {
            PINS_DRV_WritePin(PORTC, 28, 1);
            BASIC_DelayMs(50);
            PINS_DRV_WritePin(PORTC, 28, 0);
            BASIC_DelayMs(50);
        }
    } else if (frequency > 0) {
        // 低频率 - LED3慢闪1次
        PINS_DRV_WritePin(PORTC, 28, 1);
        BASIC_DelayMs(200);
        PINS_DRV_WritePin(PORTC, 28, 0);
    }

    if (frequency > 0) {
        buzzer_pwm_output(frequency, duration);
    } else {
        PINS_DRV_WritePin(BUZZER_PORT, BUZZER_PIN, 0);
        if (duration > 0) {
            Buzzer_DelayMs(duration);
        }
    }
}

/**
 * @brief 蜂鸣器停止发声
 */
void Buzzer_Stop(void)
{
    PINS_DRV_WritePin(BUZZER_PORT, BUZZER_PIN, 0);
}

/**
 * @brief 播放单个哔声
 */
void Buzzer_Beep(uint16_t frequency, uint16_t duration)
{
    Buzzer_Tone(frequency, duration);
}

/**
 * @brief 播放多个哔声
 */
void Buzzer_BeepMultiple(uint16_t frequency, uint16_t duration, uint8_t count, uint16_t interval)
{
    for (uint8_t i = 0; i < count; i++) {
        Buzzer_Tone(frequency, duration);
        if (i < count - 1) {  // 最后一个哔声后不需要间隔
            Buzzer_DelayMs(interval);
        }
    }
}

/**
 * @brief 播放旋律
 */
void Buzzer_PlayMelody(const buzzer_note_t *melody, uint16_t note_count)
{
    if (melody == NULL || note_count == 0) {
        return;
    }
    
    for (uint16_t i = 0; i < note_count; i++) {
        const buzzer_note_t *note = &melody[i];
        
        if (note->frequency == NOTE_REST) {
            // 休止符，只延时
            Buzzer_DelayMs(note->duration);
        } else {
            // 播放音符
            Buzzer_Tone(note->frequency, note->duration);
        }
        
        // 音符间隔
        Buzzer_DelayMs(50);
    }
}

/**
 * @brief 播放成功提示音
 */
void Buzzer_PlaySuccess(void)
{
    Buzzer_PlayMelody(melody_success, sizeof(melody_success) / sizeof(buzzer_note_t));
}

/**
 * @brief 播放错误提示音
 */
void Buzzer_PlayError(void)
{
    Buzzer_PlayMelody(melody_error, sizeof(melody_error) / sizeof(buzzer_note_t));
}

/**
 * @brief 播放警告提示音
 */
void Buzzer_PlayWarning(void)
{
    Buzzer_PlayMelody(melody_warning, sizeof(melody_warning) / sizeof(buzzer_note_t));
}

/**
 * @brief 播放按键音
 */
void Buzzer_PlayClick(void)
{
    Buzzer_Beep(1000, 50);
}

/**
 * @brief 播放启动音
 */
void Buzzer_PlayStartup(void)
{
    Buzzer_PlayMelody(melody_startup, sizeof(melody_startup) / sizeof(buzzer_note_t));
}

/**
 * @brief 播放两只老虎
 */
void Buzzer_PlayTwoTigers(void)
{
    Buzzer_PlayMelody(melody_two_tigers, sizeof(melody_two_tigers) / sizeof(buzzer_note_t));
}

/**
 * @brief 播放小星星
 */
void Buzzer_PlayTwinkleStar(void)
{
    Buzzer_PlayMelody(melody_twinkle_star, sizeof(melody_twinkle_star) / sizeof(buzzer_note_t));
}

/**
 * @brief 播放卡农
 */
void Buzzer_PlayCanon(void)
{
    Buzzer_PlayMelody(melody_canon, sizeof(melody_canon) / sizeof(buzzer_note_t));
}

/**
 * @brief 延时函数 (毫秒)
 */
void Buzzer_DelayMs(uint32_t ms)
{
    // 使用ESWIN SDK的延时函数
    BASIC_DelayMs(ms);
}

/**
 * @brief PWM输出控制蜂鸣器频率
 */
static void buzzer_pwm_output(uint16_t frequency, uint16_t duration)
{
    if (frequency == 0) {
        PINS_DRV_WritePin(BUZZER_PORT, BUZZER_PIN, 0);
        return;
    }

    // 改进的PWM生成方法
    // 使用毫秒级延时，更适合低频蜂鸣器
    if (frequency < 100) frequency = 100;   // 最低频率限制
    if (frequency > 5000) frequency = 5000; // 最高频率限制

    uint32_t period_ms = 1000 / frequency;  // 周期时间(毫秒)
    uint32_t half_period_ms = period_ms / 2; // 半周期时间(毫秒)

    if (half_period_ms < 1) half_period_ms = 1; // 最小1ms

    uint32_t total_cycles = duration / period_ms; // 总周期数
    if (total_cycles < 10) total_cycles = 10;  // 至少10个周期

    // 调试：LED4 (PORTD14) 闪烁表示开始PWM输出
    PINS_DRV_WritePin(PORTD, 14, 1);
    BASIC_DelayMs(50);
    PINS_DRV_WritePin(PORTD, 14, 0);

    // 生成方波 - 使用毫秒延时
    for (uint32_t i = 0; i < total_cycles; i++) {
        PINS_DRV_WritePin(BUZZER_PORT, BUZZER_PIN, 1);
        BASIC_DelayMs(half_period_ms);
        PINS_DRV_WritePin(BUZZER_PORT, BUZZER_PIN, 0);
        BASIC_DelayMs(half_period_ms);

        // 调试：每10个周期闪烁一次LED4
        if (i % 10 == 0) {
            PINS_DRV_WritePin(PORTD, 14, 1);
            BASIC_DelayMs(1);
            PINS_DRV_WritePin(PORTD, 14, 0);
        }
    }

    // 确保蜂鸣器引脚最后为低电平
    PINS_DRV_WritePin(BUZZER_PORT, BUZZER_PIN, 0);
}

/**
 * @brief 蜂鸣器简单测试函数
 */
void Buzzer_SimpleTest(void)
{
    // 测试开始指示 - 所有LED快速闪烁10次
    for (int i = 0; i < 10; i++) {
        PINS_DRV_WritePin(PORTC, 18, 1);  // LED2
        PINS_DRV_WritePin(PORTC, 28, 1);  // LED3
        PINS_DRV_WritePin(PORTD, 14, 1);  // LED4
        BASIC_DelayMs(50);

        PINS_DRV_WritePin(PORTC, 18, 0);
        PINS_DRV_WritePin(PORTC, 28, 0);
        PINS_DRV_WritePin(PORTD, 14, 0);
        BASIC_DelayMs(50);
    }

    BASIC_DelayMs(1000);

    // 测试1: LED测试 - 确认LED工作正常
    for (int i = 0; i < 3; i++) {
        PINS_DRV_WritePin(PORTC, 18, 1);  // LED2
        BASIC_DelayMs(200);
        PINS_DRV_WritePin(PORTC, 18, 0);
        BASIC_DelayMs(200);
    }

    BASIC_DelayMs(500);

    // 测试2: 蜂鸣器引脚直接控制测试
    for (int i = 0; i < 10; i++) {
        PINS_DRV_WritePin(BUZZER_PORT, BUZZER_PIN, 1);  // 高电平
        BASIC_DelayMs(100);
        PINS_DRV_WritePin(BUZZER_PORT, BUZZER_PIN, 0);  // 低电平
        BASIC_DelayMs(100);
    }

    BASIC_DelayMs(1000);

    // 测试3: 不同频率测试
    uint16_t test_frequencies[] = {200, 500, 1000, 1500, 2000};

    for (int i = 0; i < 5; i++) {
        // LED指示当前测试频率
        for (int j = 0; j <= i; j++) {
            PINS_DRV_WritePin(PORTC, 28, 1);  // LED3
            BASIC_DelayMs(100);
            PINS_DRV_WritePin(PORTC, 28, 0);
            BASIC_DelayMs(100);
        }

        BASIC_DelayMs(500);

        // 播放测试音调
        Buzzer_Tone(test_frequencies[i], 1000);
        BASIC_DelayMs(500);
    }

    // 测试完成指示 - 所有LED闪烁
    for (int i = 0; i < 5; i++) {
        PINS_DRV_WritePin(PORTC, 18, 1);  // LED2
        PINS_DRV_WritePin(PORTC, 28, 1);  // LED3
        PINS_DRV_WritePin(PORTD, 14, 1);  // LED4
        BASIC_DelayMs(200);

        PINS_DRV_WritePin(PORTC, 18, 0);
        PINS_DRV_WritePin(PORTC, 28, 0);
        PINS_DRV_WritePin(PORTD, 14, 0);
        BASIC_DelayMs(200);
    }
}

/**
 * @brief LED状态指示函数
 */
void Buzzer_LEDStatus(uint8_t pattern, uint8_t count)
{
    for (uint8_t i = 0; i < count; i++) {
        switch (pattern) {
            case 1:  // 快闪
                PINS_DRV_WritePin(PORTC, 18, 1);
                PINS_DRV_WritePin(PORTC, 28, 1);
                PINS_DRV_WritePin(PORTD, 14, 1);
                BASIC_DelayMs(100);
                PINS_DRV_WritePin(PORTC, 18, 0);
                PINS_DRV_WritePin(PORTC, 28, 0);
                PINS_DRV_WritePin(PORTD, 14, 0);
                BASIC_DelayMs(100);
                break;

            case 2:  // 慢闪
                PINS_DRV_WritePin(PORTC, 18, 1);
                PINS_DRV_WritePin(PORTC, 28, 1);
                PINS_DRV_WritePin(PORTD, 14, 1);
                BASIC_DelayMs(500);
                PINS_DRV_WritePin(PORTC, 18, 0);
                PINS_DRV_WritePin(PORTC, 28, 0);
                PINS_DRV_WritePin(PORTD, 14, 0);
                BASIC_DelayMs(500);
                break;

            case 3:  // 流水灯
                PINS_DRV_WritePin(PORTC, 18, 1);  // LED2
                BASIC_DelayMs(200);
                PINS_DRV_WritePin(PORTC, 18, 0);

                PINS_DRV_WritePin(PORTC, 28, 1);  // LED3
                BASIC_DelayMs(200);
                PINS_DRV_WritePin(PORTC, 28, 0);

                PINS_DRV_WritePin(PORTD, 14, 1);  // LED4
                BASIC_DelayMs(200);
                PINS_DRV_WritePin(PORTD, 14, 0);
                BASIC_DelayMs(200);
                break;

            case 4:  // 全亮
                PINS_DRV_WritePin(PORTC, 18, 1);
                PINS_DRV_WritePin(PORTC, 28, 1);
                PINS_DRV_WritePin(PORTD, 14, 1);
                BASIC_DelayMs(1000);
                break;

            case 5:  // 全灭
                PINS_DRV_WritePin(PORTC, 18, 0);
                PINS_DRV_WritePin(PORTC, 28, 0);
                PINS_DRV_WritePin(PORTD, 14, 0);
                BASIC_DelayMs(1000);
                break;

            default:
                break;
        }
    }
}
